package com.wwj.auction.login.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wwj.auction.login.client.OrderClient;
import com.wwj.auction.login.entity.*;
import com.wwj.auction.login.entity.vo.BuyRecordVo;
import com.wwj.auction.login.mapper.ShopMapper;
import com.wwj.auction.login.mapper.StoreMapper;
import com.wwj.auction.login.mapper.UserInfoMapper;
import com.wwj.auction.login.mapper.BuyRecordMapper;
import com.wwj.auction.login.service.BuyRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wwj
 * @since 2021-08-31
 */
@Service
public class BuyRecordServiceImpl extends ServiceImpl<BuyRecordMapper, BuyRecord> implements BuyRecordService {

    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private StoreMapper storeMapper;
    @Autowired
    private OrderClient orderClient;

    @Override
    public void create(Integer userId, Integer shopId, String orderNo) {
        // 查询买家信息
        UserInfo userInfo = userInfoMapper.selectById(userId);
        // 查询商品信息
        Shop shop = shopMapper.selectById(shopId);
        // 封装购买记录
        BuyRecord buyRecord = new BuyRecord();
        buyRecord.setShopName(shop.getShopName());
        buyRecord.setBuyerName(userInfo.getNickName());
        // 远程查询订单金额
        Map<String, Object> map = (Map<String, Object>) orderClient.getOrder(orderNo).getData().get("shopOrder");
        BigDecimal shopPrice = new BigDecimal((Double) map.get("shopPrice"));
        BigDecimal shopFreight = new BigDecimal((Double) map.get("shopFreight"));
        // 总支付金额
        BigDecimal totalPrice = shopPrice.add(shopFreight);
        buyRecord.setPayPrice(totalPrice);
        // TODO 设置物流状态，暂时均设置为了运送中
        buyRecord.setShopStatus(1);
        buyRecord.setOrderNo(orderNo);
        buyRecord.setCreateTime(LocalDateTime.now());
        // 通过商品id查询所属店铺
        Store store = storeMapper.selectById(shop.getStoreId());
        buyRecord.setStoreName(store.getStoreName());
        buyRecord.setShopIndexImage(shop.getShopIndexImage());
        buyRecord.setUserId(userId);
        buyRecord.setShopId(shopId);
        // 保存
        baseMapper.insert(buyRecord);
    }

    @Override
    public List<BuyRecordVo> get(Integer userId) {
        List<BuyRecord> buyRecordList = baseMapper.selectList(
                Wrappers.lambdaQuery(BuyRecord.class)
                        .eq(BuyRecord::getUserId, userId));
        // 封装Vo数据
        List<BuyRecordVo> buyRecordVoList = buyRecordList.stream().map(buyRecord -> {
            BuyRecordVo buyRecordVo = new BuyRecordVo();
            BeanUtils.copyProperties(buyRecord, buyRecordVo);
            return buyRecordVo;
        }).collect(Collectors.toList());
        return buyRecordVoList;
    }

    @Override
    public void status(Integer id) {
        // 查询当前购买记录
        BuyRecord buyRecord = baseMapper.selectById(id);
        // 将商品状态设置为已收货
        buyRecord.setShopStatus(2);
        // 更新
        baseMapper.updateById(buyRecord);
    }
}
